﻿using UnityEngine;
using System.Collections;

public class SparkSingleton<T> : SparkBehaviour where T : SparkBehaviour
{
    protected static T instance;

    /**
       Returns the instance of this singleton.
    */
    public static T Instance
    {
        get
        {
            if (instance == null)
            {
                instance = (T)FindObjectOfType(typeof(T));

                if (instance == null)
                {
                    //if (autoCreate)
                    //{
                    //    instance = FindObjectOfType<GameManager>().cachedGameObject.AddComponent<T>();
                    //}
                    //else
                    //{
                        Debug.LogError("An instance of " + typeof(T) +
                                               " is needed in the scene, but there is none.");
                    //}
                }
            }
            return instance;
        }
    }

    public static void DestroyInstance()
    {
        if (instance != null)
        {
            Destroy(instance);
            instance = null;
        }
    }
}

